package com.example.common.core.domain;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class R<T> {
    public int code;
    public String msg;
    public T data;

    public static <T> R<T> ok() {
        return ok(null, ResultCode.SUCCESS);
    }

    public static <T> R<T> ok(ResultCode resultCode) {
        return ok(null, resultCode);
    }

    public static <T> R<T> ok(T data) {
        return ok(data, ResultCode.SUCCESS);
    }

    public static <T> R<T> ok(T data, ResultCode resultCode) {
        //这里resultCode有可能是空
        R<T> ret = new R<>();
        ret.setData(data);
        ret.setCode(resultCode.getCode());
        ret.setMsg(resultCode.getMsg());
        return ret;
    }

    public static <T> R<T> fail() {
        return fail(null, ResultCode.FAILED);
    }

    public static <T> R<T> fail(ResultCode resultCode) {
        return fail(null, resultCode);
    }

    public static <T> R<T> fail(T data, ResultCode resultCode) {
        R<T> ret = new R<>();
        ret.setData(data);
        ret.setCode(resultCode.getCode());
        ret.setMsg(resultCode.getMsg());
        return ret;
    }
}
